#!/bin/sh
# ./omml2docx nameofdocx.docx first.omml second.omml ...
target=$1
shift
cp math-template.docx "$target"
rm -rf tmp
unzip -q -d tmp "$target"
cd tmp
doc=word/document.xml
cat >$doc <<EOF
<?xml version="1.0" encoding="utf-8" ?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<w:body>
EOF
# skip top line <?xml...
for i in "$@"
do
  echo "<w:p><w:r><w:t>$i</w:t></w:r></w:p><w:p>" >> $doc
  grep -v '[<][?]xml version' ../$i >>$doc
  echo "</w:p>" >> $doc
done
cat >>$doc <<EOF
</w:body>
</w:document>
EOF
zip -q -r ../"$target" $doc
cd ..
rm -rf tmp
echo "Created $target"
